package com.my.attendance.dao;

import com.my.attendance.entity.Employee;
import com.my.attendance.entity.EmployeeDTO;
import com.my.attendance.util.BaseDAO;

import java.util.List;

public class EmployeeDao extends BaseDAO {

    public List<EmployeeDTO> getEmployeePageList(int pageNo) throws Exception {
        String sql="select e.`id`,e.`employee_number`,e.`name`,e.`gender`,e.`telephone`,d.`name` as department_name,p.`name` as position_name,e.`in_time` \n" +
                "\tfrom employee e,department d,`position` p \n" +
                "\twhere e.department_number=d.department_number \n" +
                "\tand e.position_number=p.position_number\n" +
                "\tlimit ?,?;";
        Object[] obj={(pageNo-1)*5,5};
        List<EmployeeDTO> employeePageList = getList2(sql, new EmployeeDTO(),obj);
        return employeePageList;
    }

    public int getCount() throws Exception {
        String sql="select COUNT(1) \n" +
                "\tfrom employee e,department d,`position` p \n" +
                "\twhere e.department_number=d.department_number \n" +
                "\tand e.position_number=p.position_number;";
        int count = getCount(sql, new Object[0]);
        return count;
    }

    public int getEmployee_number() throws Exception {
        String sql="select MAX(employee_number) from employee;";
        int employee_number = getCount(sql, new Object[0]);
        return employee_number;
    }

    public int addEmployee(Employee employee) throws Exception {
        String sql="insert into employee values(null,?,?,?,?,?,?,?,?,?,?,?,?,?,?);";
        Object[] obj={employee.getEmployee_number(),employee.getName(),employee.getGender(),
                employee.getBirthday(),employee.getTelephone(),employee.getEmail(),employee.getAddress(),
                employee.getPhoto(),employee.getEducation(),employee.getDepartment_number(),
                employee.getPosition_number(),employee.getIn_time(),employee.getPassword(),employee.getNotes()};
        int i = addOrupdOrdel(sql, obj);
        return i;
    }

    public Employee getEmployee(int id) throws Exception {
        String sql="select * from employee where id=?;";
        Object[] obj={id};
        Employee employee = getOne(sql, new Employee(), obj);
        return employee;
    }

    public int updateEmployee(Employee employee) throws Exception {
        String sql="update employee set employee_number=?,name=?,gender=?,birthday=?,\n" +
                "\ttelephone=?,email=?,address=?,photo=?,education=?,\n" +
                "\tdepartment_number=?,position_number=?,in_time=?,\n" +
                "\t`password`=?,notes=? where id=?;";
        Object[] obj={employee.getEmployee_number(),employee.getName(),employee.getGender(),
                employee.getBirthday(),employee.getTelephone(),employee.getEmail(),employee.getAddress(),
                employee.getPhoto(),employee.getEducation(),employee.getDepartment_number(),
                employee.getPosition_number(),employee.getIn_time(),employee.getPassword(),
                employee.getNotes(),employee.getId()};
        int i = addOrupdOrdel(sql, obj);
        return i;
    }

    public int deleteEmployee(int id) throws Exception {
        String sql="delete from employee where id=?;";
        Object[] obj={id};
        int i = addOrupdOrdel(sql, obj);
        return i;
    }
}
